package bdgrade;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JList;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import domain.Stu;
import domain.Student;
import service.Impl.StuServiceImpl;
import tools.FileHash;
import tools.FileTool;

import javax.swing.JComboBox;
import javax.swing.JSlider;
import javax.swing.JScrollBar;
import java.awt.ScrollPane;
import java.awt.TextField;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.border.TitledBorder;
import javax.swing.event.TableColumnModelListener;
import javax.swing.UIManager;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JSeparator;
import javax.swing.ScrollPaneConstants;
import javax.swing.ScrollPaneLayout;

public class Query_Gui {
	//查询界面
	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private StuServiceImpl stuService=new StuServiceImpl();
	private JTable table;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Query_Gui window = new Query_Gui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Query_Gui() {
		initialize();
	}
	private void initialize() {		
		frame = new JFrame();
		frame.setBackground(Color.BLACK);
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Query_Gui.class.getResource("/img/1.jpg")));
		frame.setTitle("数据列表");
		frame.getContentPane().setBackground(Color.BLACK);
		frame.setBounds(100, 100, 720, 514);
		frame.getContentPane().setLayout(null);
		frame.setResizable(false);
		frame.setVisible(true);		
		//面板显示选中表行
		JPanel panel = new JPanel();
		panel.setBackground(Color.BLACK);
		panel.setBounds(0, 334, 702, 133);
		frame.getContentPane().add(panel);		
		panel.setLayout(null);
		JTextArea textArea = new JTextArea();
		textArea.setForeground(Color.WHITE);
		textArea.setBackground(Color.BLACK);
		textArea.setFont(new Font("微软雅黑", Font.BOLD, 15));
		textArea.setBounds(354, 98, 310, 24);
		panel.add(textArea);	
		JLabel label_2 = new JLabel("您已选中:");
		label_2.setForeground(Color.WHITE);
		label_2.setBounds(282, 102, 72, 18);
		panel.add(label_2);	
		//背景图
		JLabel label_3 = new JLabel("");
		label_3.setBounds(0, 0, 513, 168);
		panel.add(label_3);
		label_3.setIcon(new ImageIcon(Query_Gui.class.getResource("/img/3.jpg")));		
		//添加scrollPane,放入表格
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//加一个滑轮
		scrollPane.setBounds(0, 105, 700, 178);
		frame.getContentPane().add(scrollPane);			
		//初始化表项,标题
		Vector Title=new Vector<String>();
		Title.add("学号");
		Title.add("姓名");
		Title.add("课程");
		Title.add("班级");
		Title.add("成绩");		
		table = new JTable();		
		table.setBackground(Color.DARK_GRAY);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		table.setForeground(Color.WHITE);
		//表格信息的初始化
		{
			Vector data=new Vector<>();
			List<Stu> all = stuService.findAll();
			for(Stu s:all) {
				//设置一行
				Vector rows=new Vector<>();
				rows.add(s.getID());rows.add(s.getName());rows.add(s.getCourse());rows.add(s.getClasses());rows.add(s.getScore());
				data.add(rows);
				DefaultTableModel model = new DefaultTableModel(data,Title);
				table.setModel(model);
			}
		}
		scrollPane.setViewportView(table);//让表格在scrollPane可见
		table.addMouseListener(new MouseAdapter() {
			//监听选中表格行数
			@Override
			public void mouseClicked(MouseEvent e) {
				String se="";
				for(int i=0;i<table.getSelectedRows().length;i++) {
					int row=table.getSelectedRows()[i]+1;
					se+=row+" ";			
				}			
				textArea.setText("第"+se+"行");
			}
		});
		
		//学号面板
		JLabel label = new JLabel("\u5B66\u53F7");
		label.setForeground(Color.WHITE);
		label.setFont(new Font("学号", Font.PLAIN, 20));
		label.setBounds(14, 15, 50, 50);
		frame.getContentPane().add(label);
		//学号的输入框
		textField = new JTextField();
		textField.setBounds(78, 30, 150, 25);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		//课程面板
		JLabel label_1 = new JLabel("\u8BFE\u7A0B");
		label_1.setForeground(Color.WHITE);
		label_1.setFont(new Font("课程", Font.PLAIN, 20));
		label_1.setBounds(258, 15, 50, 50);
		frame.getContentPane().add(label_1);	
		//课程的输入框
		textField_1 = new JTextField();
		textField_1.setBounds(322, 30, 150, 25);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		//查询按钮键
		JButton button = new JButton("查询");
		button.setFont(new Font("宋体", Font.PLAIN, 15));
		button.setBackground(Color.BLACK);
		button.setForeground(Color.WHITE);
		button.setBounds(486, 28, 73, 37);
		frame.getContentPane().add(button);

		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Vector data = new Vector<>();
				//点击了查询的按钮发生的事件
				//需要对输入的内容进行搜索
				String id_str = textField.getText();//获取学号框的内容
				String course_str = textField_1.getText();//获取课程框的内容
				//修改版:
				int id = 0;
				if (id_str != null&&!id_str.equals("")&&id_str.length()!=0) id = Integer.parseInt(id_str);
				if(course_str.equals("")||course_str.length()==0)course_str=null;
				Stu stu = new Stu();
				stu.setID(id);
				stu.setCourse(course_str);
				List<Stu> byIdOrCourse = stuService.findByIdOrCourse(stu);
				{
					data.clear();
					for (Stu s : byIdOrCourse) {
						//设置一行
						Vector rows = new Vector<>();
						rows.add(s.getID());
						rows.add(s.getName());
						rows.add(s.getCourse());
						rows.add(s.getClasses());
						rows.add(s.getScore());
						data.add(rows);
						DefaultTableModel model = new DefaultTableModel(data, Title);
						table.setModel(model);
					}
				}
			}
		});

				//修改按键,点击跳转修改界面
				JButton button_1 = new JButton("修改");
				button_1.setFont(new Font("宋体", Font.PLAIN, 15));
				button_1.setBounds(551, 28, 76, 37);
				frame.getContentPane().add(button_1);
				button_1.setForeground(Color.WHITE);
				button_1.setBackground(Color.BLACK);
				button_1.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						//点击修改按钮
						if (table.getSelectedRowCount() > 1) {
							new Ch_tip();//一次只能修改一个,如果选中多个就提示不能选择多个修改
						} else {
							int row = table.getSelectedRow();
							if (table.getSelectedRow() == -1) row = 1;//默认值设置为1,放置误触
//					frame.dispose();

							new Change(row);//跳转到修改界面
						}
					}
				});

				//删除按键,点击跳转删除提示界面
				JButton btnNewButton = new JButton("删除");
				btnNewButton.setFont(new Font("宋体", Font.PLAIN, 15));
				btnNewButton.setForeground(Color.WHITE);
				btnNewButton.setBackground(Color.BLACK);
				btnNewButton.setBounds(626, 28, 76, 37);
				frame.getContentPane().add(btnNewButton);
				btnNewButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						for (int r : table.getSelectedRows()) {
							System.out.println(r);
						}
						//点击删除按钮
						new IsOK(table.getSelectedRows());//将选中行数组赋予它
					}
				});

				//刷新按钮,主要功能是更新当前有变动的数据信息
				JButton button_2 = new JButton("刷新");
				button_2.setFont(new Font("宋体", Font.PLAIN, 15));
				button_2.setForeground(Color.WHITE);
				button_2.setBackground(Color.BLACK);
				button_2.setBounds(616, 0, 72, 35);
				panel.add(button_2);
				
				JButton btnNewButton_1 = new JButton("统计");
				btnNewButton_1.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						new Statistics();
					}
				});
				btnNewButton_1.setForeground(Color.WHITE);
				btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 15));
				btnNewButton_1.setBackground(Color.BLACK);
				btnNewButton_1.setBounds(616, 48, 72, 35);
				panel.add(btnNewButton_1);
				button_2.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						frame.dispose();//关闭当前窗口
						new Query_Gui();//重新打开该窗口
					}
				});
			}
		}

